package com.veer.shop.service.impl.admin;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.veer.shop.config.ResponseCodeEnum;
import com.veer.shop.mapper.AttributeSetMapper;
import com.veer.shop.param.AttributeSetParam;
import com.veer.shop.pojo.AttributeSet;
import com.veer.shop.pojo.ResponseData;
import com.veer.shop.pojo.ValidResult;
import com.veer.shop.service.admin.AttributeSetService;
import com.veer.shop.utils.DateTimeUtil;
import com.veer.shop.utils.InitUtil;
import com.veer.shop.utils.ResponseUtil;

/**
 * 商品属性集业务实现类
 * 
 * @author WangBin
 * 2020年9月22日-上午9:45:59
 */
@Service
public class AttributeSetServiceImpl implements AttributeSetService {
	
	@Autowired
	AttributeSetMapper attributeSetMapper;

	/**
	 * 新增|更新属性集
	 */
	@Override
	public ResponseData<?> update(AttributeSet attributeSet) {
		// 校验参数
		ValidResult verifyResult = verify(attributeSet);
		int count = 0;
		if(verifyResult.getCode() == 0) {
			if(attributeSet.getAttributeSetId() > 0) {
				// 更新属性集
				count = attributeSetMapper.update(attributeSet);
			} else {
				// 新增属性集
				attributeSet.setCreatedAt(DateTimeUtil.getDateTime());
				attributeSet.setUpdatedAt(attributeSet.getCreatedAt());
				
				count = attributeSetMapper.insert(attributeSet);
			}
			
			if(count > 0) {
				return ResponseUtil.success();
			} else {
				return ResponseUtil.error();
			}
		} else {
			return ResponseUtil.error(verifyResult.getMessage());
		}
	}

	/**
	 * 属性集列表
	 */
	@Override
	public ResponseData<?> getList(AttributeSetParam param, Integer pageNo, Integer pageSize) {
		Map<String, Object> listResultMap = InitUtil.initListResultMap();
		Page page = PageMethod.startPage(pageNo, pageSize);
		List<AttributeSet> result = attributeSetMapper.selectAll(param);
		listResultMap.put("count", page.getTotal());
		listResultMap.put("list", result);
		return ResponseUtil.success(listResultMap);
	}

	/**
	 * 删除属性集
	 */
	@Override
	public ResponseData<?> delete(String setId) {
		
		// 处理属性集ID
		String[] brandIdList = setId.split(",");
		for(String id : brandIdList) {
			int bid = Integer.parseInt(id);
			
			// 查询属性集
			AttributeSet findResult = attributeSetMapper.selectById(bid);
			if(findResult == null)continue;
			
			attributeSetMapper.delete(bid);
		}
		
		return ResponseUtil.success();
	}

	/**
	 * 属性集详情
	 */
	@Override
	public ResponseData<?> get(Integer setId) {
		// 查询属性集
		AttributeSet findResult = attributeSetMapper.selectById(setId);
		
		if(findResult == null)return ResponseUtil.error(ResponseCodeEnum.VIEW_ATTRIBUTE_SET_ERROR.getMessage());
		
		return ResponseUtil.success(findResult);
	}

	/**
	 * 校验属性集参数合法性
	 * 
	 * @param attributeSet
	 * @return
	 */
	private ValidResult verify(AttributeSet attributeSet) {
		
		ValidResult result = InitUtil.initFaildMap();
		
		if(attributeSet.getAttributeSetName().equals("")) {
			result.setMessage("名称不可为空！");
			return result;
		}
		
		if(attributeSet.getAttributeSetBn().equals("")) {
			result.setMessage("编码不可为空！");
			return result;
		}
		
		if(attributeSet.getAttributeSetId() > 0) {
			// 查询属性集
			AttributeSet findAttributeSet = attributeSetMapper.selectById(attributeSet.getAttributeSetId());
			
			if(findAttributeSet == null) {
				result.setMessage("属性集不存在！");
				return result;
			}
		}
		
		AttributeSetParam param = new AttributeSetParam();
		param.setAttributeSetBn(attributeSet.getAttributeSetBn());
		List<AttributeSet> findResult = attributeSetMapper.selectAll(param);
		
		if(findResult.size() > 0) {
			result.setMessage("名称或编码已存在！");
			return result;
		}
		
		return InitUtil.initSuccMap();
	}
}
